package com.spring.boot.demo.config;

import com.spring.boot.demo.interceptor.CorsInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/10/31 22:58
 * @Description CORS 跨域配置类
 */
//@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域访问的信息
        registry.addMapping("/**") // 拦截哪些请求
                .allowCredentials(true)
                .allowedOrigins("*") // 允许哪些域访问，*表示所有
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许哪些请求方式跨域访问
                .maxAge(3600); // 设置请求缓存时间
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CorsInterceptor())
                .addPathPatterns("/**");
    }
}
